package day04;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

@WebServlet("*.code")
public class CodeServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = request.getServletPath();

        if ("/show.code".equals(path)) {
            show(request,response);
        }

        if ("/submit.code".equals(path)) {
            submit(request,response);
        }
    }

    protected void submit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CodeServlet.submit()");
        HttpSession session=request.getSession();

        String code=request.getParameter("code");
        String sessionCode= (String) session.getAttribute("code");

        if(code!=null&&!"".equals(code.trim())&&code.equalsIgnoreCase(sessionCode)){

            session.removeAttribute("code");

            response.sendRedirect(request.getContextPath()+"/hello.jsp");
            return;
        }

        request.setAttribute("message","验证码错误");
        request.getRequestDispatcher("/code.jsp").forward(request,response);

    }


    protected void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CodeServlet.show()");

        Random random=new Random();

        //在内存中创建一个画图版
        BufferedImage image=new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);

        //获得画笔
        Graphics graphics=image.getGraphics();

        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),
                                    random.nextInt(256),
                                    random.nextInt(256)));


        //画一个带填充色的矩形作为背景
        graphics.fillRect(0,0,70,30);

        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),
                                    random.nextInt(256),
                                    random.nextInt(256)));

        //设置画笔字体
        graphics.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,18));

        //生成一个5位随机字符串，字符串中包括[a-z][A-Z][0-9]
        String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        StringBuilder code=new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int index=random.nextInt(s.length());
            code.append(s.charAt(index));
        }

        request.getSession().setAttribute("code",code.toString());
        //将文字写到图片上
        graphics.drawString(code.toString(),5,20);

        //画3条干扰线
        for (int i = 0; i < 3; i++) {
            //设置画笔颜色
            graphics.setColor(new Color(random.nextInt(256),
                                        random.nextInt(256),
                                        random.nextInt(256)));

            //画线
            graphics.drawLine(random.nextInt(71),
                              random.nextInt(31),
                              random.nextInt(71),
                              random.nextInt(31));
        }

        //画3个干扰点
        for (int i = 0; i < 3; i++) {
            //设置画笔颜色
            graphics.setColor(new Color(random.nextInt(256),
                                        random.nextInt(256),
                                        random.nextInt(256)));

            //画圆
            graphics.fillOval(random.nextInt(71),
                             random.nextInt(31),
                             3,
                             3);

            OutputStream out=response.getOutputStream();
            ImageIO.write(image,"jpeg",out);
        }


    }
}
